package Dao;

import DataClass.Announcement;
import Util.JBDCUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AnnouncementDao {

    private static PreparedStatement preparedStatement1 = null;
    private static PreparedStatement preparedStatement2 = null;
    private static PreparedStatement preparedStatement3 = null;

    public static List<Announcement> getAnnouncement1() throws SQLException {
        List<Announcement> list = new ArrayList<Announcement>();
        ResultSet data;
        try {
            data = AnnouncementDao.getAnnouncement1Data();
        } catch (SQLException e) {
            System.out.println("动态信息失败 from Dao");
            throw new SQLException();
        }

        while (data.next()) {

            String ATitle = data.getString("ATitle");
            String ATime = data.getString("ATime");
            String AContent = data.getString("AContent");
            list.add(new Announcement(ATitle, ATime, AContent));
        }

        // 释放资源
        JBDCUtil.closeResultSet(data);
        return list;
    }
    public static List<Announcement> getAnnouncement2() throws SQLException {
        List<Announcement> list = new ArrayList<Announcement>();
        ResultSet data;
        try {
            data = AnnouncementDao.getAnnouncement2Data();
        } catch (SQLException e) {
            System.out.println("动态信息失败 from Dao");
            throw new SQLException();
        }

        while (data.next()) {

            String ATitle = data.getString("ATitle");
            String ATime = data.getString("ATime");
            String AContent = data.getString("AContent");
            list.add(new Announcement(ATitle, ATime, AContent));
        }

        // 释放资源
        JBDCUtil.closeResultSet(data);

        return list;
    }
    public static List<Announcement> getAnnouncement3() throws SQLException {
        List<Announcement> list = new ArrayList<Announcement>();
        ResultSet data;
        try {
            data = AnnouncementDao.getAnnouncement3Data();
        } catch (SQLException e) {
            System.out.println("动态信息失败 from Dao");
            throw new SQLException();
        }

        while (data.next()) {

            String ATitle = data.getString("ATitle");
            String ATime = data.getString("ATime");
            String AContent = data.getString("AContent");
            list.add(new Announcement(ATitle, ATime, AContent));
        }

        // 释放资源
        JBDCUtil.closeResultSet(data);

        return list;
    }

    /**
     * 获取新闻
     */
    public static ResultSet getAnnouncement1Data() throws SQLException{
        if(preparedStatement1 == null)
        {
            String sql = "SELECT ATitle, ATime, AContent FROM Announcement WHERE AType = '通知公告' ORDER BY AID DESC LIMIT 3";
            // 设置statement对象，解析SQL语句，并且保存，节省重复语句解析开销
            preparedStatement1 = JBDCUtil.getPreparedStatement(sql);
        }

        ResultSet data = preparedStatement1.executeQuery();
        JBDCUtil.commit();
        return data;
    }

    /**
     * 获取通知公告
     */
    public static ResultSet getAnnouncement2Data() throws SQLException{
        if(preparedStatement2 == null)
        {
            String sql = "SELECT ATitle, ATime, AContent FROM Announcement WHERE AType = '新闻动态' ORDER BY AID DESC LIMIT 3";
            // 设置statement对象，解析SQL语句，并且保存，节省重复语句解析开销
            preparedStatement2 = JBDCUtil.getPreparedStatement(sql);
        }

        ResultSet data = preparedStatement2.executeQuery();
        JBDCUtil.commit();
        return data;
    }

    /**
     * 获取学术活动
     */
    public static ResultSet getAnnouncement3Data() throws SQLException{
        if(preparedStatement3 == null)
        {
            String sql = "SELECT ATitle, ATime, AContent FROM Announcement WHERE AType = '学术活动' ORDER BY AID DESC LIMIT 3";
            // 设置statement对象，解析SQL语句，并且保存，节省重复语句解析开销
            preparedStatement3 = JBDCUtil.getPreparedStatement(sql);
        }

        ResultSet result = preparedStatement3.executeQuery();
        JBDCUtil.commit();

        return result;
    }
}
